package cn.wolfcode.rbac.exception;

import cn.wolfcode.rbac.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Lu.F
 * @version 1.0
 * @Description TODO
 * @date 2022/5/14 16:05
 */
@RestControllerAdvice
@Slf4j
public class CustomExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    public JsonData handler(Exception e){

        if (e instanceof CustomException){
            CustomException customException = (CustomException) e;
            log.error("自定义异常错误{}",customException);
            return JsonData.buildError(customException.getCode(), customException.getMsg());
        }else {
            log.error("全局异常错误{}",e);
            return JsonData.buildError("系统错误，全局异常");
        }
    }

}
